//
//  CollectController.m
//  GC_OC
//
//  Created by 叶建辉 on 2022/7/11.
//

#import "CollectController.h"

@interface CollectController ()<CollectViewDelegate,classViewDelegate,GCAVPlayerDelegate>
@property(nonatomic,strong)CollectView *cView;
@property(nonatomic,strong)classView *showView;
@property (nonatomic, strong) ObjectAFNetWorking* objectAFN;
@property (nonatomic, strong) NSString* yj_id;
@property (nonatomic, strong) NSString* ej_id;
@property (nonatomic, strong) NSArray* datas;
@property (nonatomic, assign) BOOL isAZ;
@end

@implementation CollectController

-(NSString *)yj_id{
    if (!_yj_id) {
        _yj_id = @"";
    }
    return _yj_id;
}

-(NSString *)ej_id{
    if (!_ej_id) {
        _ej_id = @"";
    }
    return _ej_id;
}

-(void)playforMedia_id:(NSString *)media_id{
    for (int i = 0; i < _cView.datas.count; i++) {
        NSString *str = [NSString setSafeString:_cView.datas[i][@"media_id"]];
        if ([str isEqualToString:media_id]) {
            _cView.selectNum = i;
            break;
        }
    }
}

-(void)ClickOnTheIndexPathRow:(NSInteger)section{
    if ([GCAVPlayer shared].delegate != self) {
        [GCAVPlayer shared].delegate = self;
    }
    [[GCAVPlayer shared] play:_cView.datas andindex:section ViewStatue:1 andIsAudio:YES];
    
}

-(void)DeleteOnTheIndexPathRow:(NSInteger)section{
    NSString *url = [NSString stringWithFormat:@"%@/api/collect/setCollect",server_addr];
    NSString *media_id = _cView.datas[section][@"media_id"];
    [_objectAFN postUrlStr:url parames:@{@"media_id":media_id,@"user_id":[[Cache shared] user_id]} finish:^(id  _Nonnull dataString) {
        NSDictionary* detailsDic=[NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:nil];
        if ([[NSString setSafeString:detailsDic[@"code"]] intValue] == 1) {
            [self getData];
        }else{
            
        }
    } andError:^(NSError * _Nonnull error) {
            
    }];
}

-(void)yjClickOnTheIndexPathRow:(NSInteger)section{
    //处理二级列表跟收藏列表
    NSString *str = _showView.yjdatas[section];
    [_showView.ejdatas removeAllObjects];
    if ([str isEqualToString:@"全部"]) {
        [_showView.ejdatas addObject:@"不限"];
        for (NSDictionary *dic in _datas) {
            if (![_showView.ejdatas containsObject:[NSString setSafeString:dic[@"category_name"]]]) {
                [_showView.ejdatas addObject:[NSString setSafeString:dic[@"category_name"]]];
            }
        }
        _showView.ejselectNum = 0;
        [_showView.ejtableView reloadData];
        
        _cView.datas = _datas.mutableCopy;
        
        
    }else{
        [_showView.ejdatas addObject:@"不限"];
        for (NSDictionary *dic in _datas) {
            if ([str isEqualToString:[NSString setSafeString:dic[@"category_name_1"]]]) {
                if (![_showView.ejdatas containsObject:[NSString setSafeString:dic[@"category_name"]]]) {
                    [_showView.ejdatas addObject:[NSString setSafeString:dic[@"category_name"]]];
                }
            }
        }
        
        _showView.ejselectNum = 0;
        [_showView.ejtableView reloadData];
        
        NSMutableArray *arrs = [NSMutableArray array];
        for (NSDictionary *dic in _datas) {
            if ([str isEqualToString:[NSString setSafeString:dic[@"category_name_1"]]]) {
                if (![arrs containsObject:dic]) {
                    [arrs addObject:dic];
                }
            }
        }
        _cView.datas = arrs;
    }
    
    
    
}

-(NSMutableArray *)getzdxData:(NSMutableArray *)data{
    if (_isAZ) {
        return data;
    }else{
        return [[data reverseObjectEnumerator] allObjects].mutableCopy;
    }
    return data;
}

-(void)ejClickOnTheIndexPathRow:(NSInteger)section{
    //只处理收藏列表
    NSString *str = _showView.ejdatas[section];
    NSString *str1 = _showView.yjdatas[_showView.yjselectNum];
    if ([str isEqualToString:@"不限"]) {
        NSMutableArray *arrs = [NSMutableArray array];
        for (NSDictionary *dic in _datas) {
            if ([str1 isEqualToString:@"全部"]) {
                if (![arrs containsObject:dic]) {
                    [arrs addObject:dic];
                }
            }else{
                if ([str1 isEqualToString:[NSString setSafeString:dic[@"category_name_1"]]]){
                    if (![arrs containsObject:dic]) {
                        [arrs addObject:dic];
                    }
                }
            }
            
        }
        _cView.datas = [self getzdxData:arrs];
    }else{
        NSMutableArray *arrs = [NSMutableArray array];
        for (NSDictionary *dic in _datas) {
            if ([str1 isEqualToString:@"全部"]){
                if ([str isEqualToString:[NSString setSafeString:dic[@"category_name"]]]) {
                    if (![arrs containsObject:dic]) {
                        [arrs addObject:dic];
                    }
                }
            }else{
                if ([str1 isEqualToString:[NSString setSafeString:dic[@"category_name_1"]]]){
                    if ([str isEqualToString:[NSString setSafeString:dic[@"category_name"]]]) {
                        if (![arrs containsObject:dic]) {
                            [arrs addObject:dic];
                        }
                    }
                }
            }
        }
        _cView.datas = [self getzdxData:arrs];
    }
}

-(CollectView *)cView{
    if (!_cView) {
        _cView = [CollectView new];
        _cView.delegate = self;
    }
    return _cView;
}

-(classView *)showView{
    if (!_showView) {
        _showView = [classView new];
        _showView.hidden = YES;
        _showView.delegate = self;
        _showView.layer.zPosition = 20;
    }
    return _showView;
}

-(void)ClickOnTheButtonTag:(NSInteger)tag{
    if (tag == 0) {
        [_showView removeFromSuperview];
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if (tag == 1){
        _showView.hidden = !_showView.hidden;
        [_cView changeshowView:!_showView.hidden];
    }
    else if (tag == 2){
        _isAZ = !_isAZ;
        [_cView changeazView:_isAZ];
        _cView.datas = [[_cView.datas reverseObjectEnumerator] allObjects].mutableCopy;
    }
}

-(void)hideSelf{
    [_cView changeshowView:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.cView];
    [[UIApplication sharedApplication].keyWindow addSubview:self.showView];
    _objectAFN = [ObjectAFNetWorking new];
    
    [self getData];
    if ([[NSUserDefaults standardUserDefaults] integerForKey:@"issequence"]==0) {
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"issequence"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        _isAZ = YES;
    }else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"issequence"]==1) {
        [[NSUserDefaults standardUserDefaults] setInteger:2 forKey:@"issequence"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        _isAZ = NO;
    }else if ([[NSUserDefaults standardUserDefaults] integerForKey:@"issequence"]==2){
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"issequence"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        _isAZ = YES;
    }
}

-(void)getData{
    NSString *url = [NSString stringWithFormat:@"%@/api/collect/getCollect",server_addr];
    NSLog(@"detailsDic~~>>%@",url);
    [_objectAFN postUrlStr:url parames:@{@"user_id":[[Cache shared] user_id],@"yj_id":self.yj_id,@"ej_id":self.ej_id} finish:^(id  _Nonnull dataString) {
        NSDictionary* detailsDic=[NSJSONSerialization JSONObjectWithData:dataString options:NSJSONReadingMutableContainers error:nil];

        NSLog(@"getonecollect~~~>>%@",detailsDic);
        
        self->_cView.datas = [self getzdxData:[NSArray setSafeArr:detailsDic[@"data"][@"data"]].mutableCopy];
        
        self->_datas = [NSArray setSafeArr:detailsDic[@"data"][@"data"]];
        [self initclassData];
        } andError:^(NSError * _Nonnull error) {
            NSLog(@"~~~~1`~>>%@",error);
    }];
}

-(void)initclassData{
//    _showView.yjdatas
    [_showView.yjdatas addObject:@"全部"];
    for (NSDictionary *dic in _cView.datas) {
        if (![_showView.yjdatas containsObject:[NSString setSafeString:dic[@"category_name_1"]]]) {
            [_showView.yjdatas addObject:[NSString setSafeString:dic[@"category_name_1"]]];
        }
    }
    [_showView.yjtableView reloadData];
    
    [_showView.ejdatas addObject:@"不限"];
    for (NSDictionary *dic in _cView.datas) {
        if (![_showView.ejdatas containsObject:[NSString setSafeString:dic[@"category_name"]]]) {
            [_showView.ejdatas addObject:[NSString setSafeString:dic[@"category_name"]]];
        }
    }
    [_showView.ejtableView reloadData];
}

-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    _cView.frame = CGRectMake(0, 0, WIDTHDiv, HEIGHTDiv);
    _showView.frame = CGRectMake(0, (isIphoneX ? 88.0 : 64.0), WIDTHDiv, HEIGHTDiv);
}

-(void)dealloc{
    if (!_isAZ) {
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"issequence"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }else{
        [[NSUserDefaults standardUserDefaults] setInteger:2 forKey:@"issequence"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

@end
